function returnValue =  birdFFT(filename,a)
% Open file and use fft to plot a graph
% file name as input
%wavFileName=strcat(filename,'.wav');

%read the file and use a fft operation
%this is modified from the MATLAB official manual of fft function
wavFileName=filename;
[x,fs]=audioread(wavFileName);
%sound(x,fs);
%plot(x);
%xlabel("Time");
%ylabel('Amplitude');
m=length(x);
n=pow2(nextpow2(m));
y=fft(x,n);
power=abs(y).^2/n;
f=(0:n-1)*(fs/n);

% plot the graph, to show whether some error exist in data
subplot(4,4,a)
plot(f(1:floor(n/2)),power(1:floor(n/2)));
disp(filename);

%find the 3000Hz bound index
for index = 1:floor(n/2) 
    if f(index)>2000 && f(index) <2002
        lowerBound = index;
        break;
    end
end

%find the 7000Hz bound index
for index = 1:floor(n/2)
    if f(index)>7000 && f(index) <7002
        higherBound = index;
        break;
    end
end
%disp(lowerBound);
%disp(higherBound);
%[maxPower,maxPowerIndex] = max(power(lowerBound:higherBound,1));
maxPower=0;

%find the peak frequency
for index = lowerBound : higherBound
    if maxPower < power(index,1)
        maxPowerIndex = index;
        maxPower=power(index,1);
    end
end

maxPowerFrequency = f(maxPowerIndex);

%find the lower bound frequency
for index = lowerBound:higherBound
    if power(index,1)> (1/3)*maxPower
        leftPower= power(index,1);
        leftFrequency = f(index);
        leftIndex= index;
        break;
    end
end

%find the upper bound frequency
for index = higherBound:-1:lowerBound
    if power(index,1)>(1/3)*maxPower
        rightPower = power(index,1);
        rightFrequency = f(index);
        rightIndex = index;
        break;
    end
end

disp(leftFrequency);
disp(maxPowerFrequency);
disp(rightFrequency);

% store the data 
goodData(a,1)=leftFrequency;
goodData(a,2)=maxPowerFrequency;
goodData(a,3)=rightFrequency;

%used for debug

%disp(leftPower);
%disp(maxPower);
%disp(rightPower);
%disp(leftIndex);
%disp(maxPowerIndex);
%disp(rightIndex);


xlabel("Frequency");
ylabel('Relative Power');
title(filename)

%box off;

%can choose to save the picture generated

%savePicFileName=strcat(filename,'.fig');
%saveas(gcf,savePicFileName,'fig');
returnValue = [leftFrequency,maxPowerFrequency,rightFrequency];
end